package cn.walls1717.wallsmall.ware.controller;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.ware.entity.WareSkuEntity;
import cn.walls1717.wallsmall.ware.exception.NoStockException;
import cn.walls1717.wallsmall.ware.pojo.vo.SkuHasStockVO;
import cn.walls1717.wallsmall.ware.pojo.vo.WareSkuLockVO;
import cn.walls1717.wallsmall.ware.service.WareSkuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cn.walls1717.wallsmall.common.exception.BizCodeEnum.NO_STOCK_EXCEPTION;

/**
 * 商品库存
 *
 * @author walls1717
 * @email walls1717@163.com
 * @date 2022-10-21 13:26:06
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Resource
    private WareSkuService wareSkuService;

    /** 锁定库存 */
    @PostMapping("/lock/order")
    public R orderStockLock(@RequestBody WareSkuLockVO stockLock) {
        try {
            wareSkuService.orderStockLock(stockLock);
            return R.ok();
        } catch (NoStockException e) {
            return R.error(NO_STOCK_EXCEPTION.getCode(), NO_STOCK_EXCEPTION.getMsg());
        }

    }

    /**
     * 查询sku是否有库存
     */
    @PostMapping("/hasstock")
    public List<SkuHasStockVO> getHasStock(@RequestBody List<Long> skuIds) {
        return wareSkuService.getHasStock(skuIds);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.listPagesByConditions(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("ware:waresku:save");
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
